package com.smbms.controller;

import com.mysql.cj.util.StringUtils;
import com.smbms.pojo.User;
import com.smbms.service.UserService;
import com.smbms.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @GetMapping(value = "/login.html")
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/login.html", method = RequestMethod.POST)
    public String doLogin(@RequestParam String userCode, @RequestParam String userPassword, Model model,
                          HttpSession session) throws Exception {
        // 调用service方法，进行用户匹配
        User user = userService.login(userCode, userPassword);

        if (null != user) {// 登录成功
            if (user.getUserPassword().equals(userPassword)) {
                // 放入session
                session.setAttribute(Constants.USER_SESSION, user);
                // 页面跳转（frame.jsp）
                return "redirect:/main.html";
            } else {
                // 页面跳转（login.jsp）带出提示信息--转发
                model.addAttribute("error", "登录失败，密码不正确！");
                model.addAttribute("userCode", userCode);
                return "login";
            }
        } else {
            // 页面跳转（login.jsp）带出提示信息--转发
            model.addAttribute("error", "登录失败，用户不存在！");
            return "login";
        }
    }
   @GetMapping(value = "/logout.html")
    public String logout(HttpSession session) {
        // 清除session
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }

    @RequestMapping(value = "/main.html")
    public String main() {
        return "frame";
    }

    @GetMapping(value = "logexit.html")
    public String logexit(HttpSession session) {
        // 清除session
        session.removeAttribute(Constants.USER_SESSION);
        return "login";
    }
    @GetMapping(value = "/pwdmodify.html")
    public String pwdmodify() {

        return "pwdmodify";
    }
    @RequestMapping(value = "/pwdmod", method = RequestMethod.GET)
    @ResponseBody
    public Object pwdmod(@RequestParam String oldpassword, HttpServletRequest request) {
        Map<String, String> resultMap = new HashMap<String, String>();
        if (request.getSession().getAttribute("userSession") == null) {
            resultMap.put("result", "sessionerror");
        } else {
            User loginUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            if (loginUser.getUserPassword().equals(oldpassword)) {
                resultMap.put("result", "true");
            } else if (!loginUser.getUserPassword().equals(oldpassword)) {
                resultMap.put("result", "false");
            } else {
                resultMap.put("result", "error");
            }
        }
        return resultMap;
    }
    @RequestMapping(value = "/pwdmod.html", method = RequestMethod.POST)
    public String pwdmodify(@RequestParam String newpassword, HttpServletRequest request) {
        if (StringUtils.isNullOrEmpty(newpassword)) {
            return "/pwdmodify.html";
        } else {
            User loginUser = (User) request.getSession().getAttribute(Constants.USER_SESSION);
            boolean result = userService.updatePwd(loginUser.getId(),newpassword);
            if (result) {
                return "login";
            }
            return "pwdmodify";
        }
    }
}
